package com.hxminer.hxwebs.service;

import com.hxminer.hxwebs.dao.CartDao;
import com.hxminer.hxwebs.enmus.ResultEnum;
import com.hxminer.hxwebs.exceptions.HxException;
import com.hxminer.hxwebs.model.Orders;
import com.hxminer.hxwebs.model.Product;
import com.hxminer.hxwebs.model.Result;
import com.hxminer.hxwebs.repository.CartRepository;
import com.hxminer.hxwebs.repository.OrdersRepository;
import com.hxminer.hxwebs.utils.ResultUitl;
import com.sun.org.apache.bcel.internal.generic.IFNULL;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Author YRG
 * Created by on 2018/5/15.
 * Describe
 */

@Service
public class CartService {


    @Autowired
    CartDao cartDao;

    @Autowired
    CartRepository cartRepository;

    /**
     * 插入数据库
     *
     * @param product
     */
    public Result<String> saveCartProduct(Product product) throws Exception {
        if (product == null || "".equals(product.getUserId()) || null == product.getUserId()) {
            throw new HxException(ResultEnum.PRODUCT_TYPE);
        }
        List<Product> products = cartDao.findByUserIdAndProductType(product.getUserId(), product.getProductType());
        if (products == null || products.size() <= 0) {
            cartRepository.save(product);
            return ResultUitl.success(ResultEnum.CART_ADD_SUCCESS);//插入成功
        } else {
            Product product1 = products.get(0);
            product1.setUserId(product.getUserId());
            product1.setProductName(product.getProductName());
            product1.setProductType(product.getProductType());
            product1.setProductTime(product.getProductTime());
            product1.setProductNo(product.getProductNo());
            product1.setProductAmount(product.getProductAmount());
            product1.setPrice(product.getPrice());
            product1.setTypeTotalPrice(product.getTypeTotalPrice());
            product1.setAllPrice(product.getAllPrice());
            cartRepository.save(product1);
            return ResultUitl.success(ResultEnum.CART_ADD_SUCCESS);//更新数据
        }
        //判断是否有数据在数据库
    }

    /**
     * 获取购物车数据
     *
     * @param userId
     * @return
     * @throws Exception
     */
    public Result<Product> findByCartProduct(String userId) throws Exception {
        List<Product> products = cartDao.findByUserId(userId);
        if (products == null || products.size() <= 0) {
            throw new HxException(ResultEnum.PRODUCT_CART_ERROR);
        }
        return ResultUitl.success(ResultEnum.PRODUCT_CART_SUCCESS, products);
    }


    public Result<String> deleteCartProduct(Product product) throws Exception {

        String userId = product.getUserId();
        String productType = product.getProductType();

        List<Product> products = cartDao.findByUserIdAndProductType(userId, productType);
        if (products == null && products.size() <= 0) {
            return ResultUitl.error(ResultEnum.UNKONW_ERROR);
        }
        cartRepository.delete(products.get(0));
        return ResultUitl.success(ResultEnum.SUCCESS);

    }


}
